from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
from werkzeug.security import generate_password_hash
from apps import db
from apps import app
from apps.cms import models as cms_models
from apps.front import models as front_models
from apps.cms.models import CMSRole, CMSPermission, CMSUser
from apps.front.models import FrontUser

manager = Manager(app=app)
Migrate(app=app, db=db)
manager.add_command('db', MigrateCommand)


# 创建角色
@manager.command
def create_role():
    """创建相关的角色"""
    visitor = CMSRole(name='访问者', desc='只能查看相关数据，不能修改', permissions=CMSPermission.VISITER)
    operator = CMSRole(name='运营', desc='管理帖子，管理评论，管理前台用户',
                       permissions=CMSPermission.VISITER | CMSPermission.POSTER | CMSPermission.COMMENTER | CMSPermission.FRONTUSER)
    admin = CMSRole(name='管理员', desc='拥有所有权限',
                    permissions=CMSPermission.VISITER | CMSPermission.POSTER | CMSPermission.COMMENTER | CMSPermission.BOARDER | CMSPermission.FRONTUSER)
    developer = CMSRole(name='开发者', desc='拥有所有权限',
                        permissions=CMSPermission.VISITER | CMSPermission.POSTER | CMSPermission.COMMENTER | CMSPermission.FRONTUSER | CMSPermission.CMSUSER | CMSPermission.BOARDER)
    db.session.add_all([visitor, operator, admin, developer])
    db.session.commit()


# 检查cms用户有没有权限
@manager.command
def test_permission():
    user = CMSUser.query.first()
    if user.has_permission(CMSPermission.VISITER):
        print('这个用户有访问的权限')
    else:
        print('这个用户没有访问的权限')


# 添加cms用户和角色的关系
@manager.option('-e', '--email', dest='email')
@manager.option('-n', '--name', dest='name')
def role_to_user(email, name):
    user = CMSUser.query.filter_by(email=email).first()
    if not user:
        print('此邮箱对应的用户不存在')
    role = CMSRole.query.filter_by(name=name).first()
    if not role:
        print('此名字的角色不存在')
    role.users.append(user)
    db.session.commit()
    print('添加成功')


# 添加cms用户
@manager.option('-n', '--name', dest='name')
@manager.option('-p', '--password', dest='password')
@manager.option('-e', '--email', dest='email')
def create_cms_user(name, password, email):
    user = CMSUser(username=name, password=password, email=email)
    db.session.add(user)
    db.session.commit()
    print('cms用户添加成功')


@manager.option('-n', '--username', dest='username')
@manager.option('-p', '--password', dest='password')
@manager.option('-e', '--email', dest='email')
# @manager.option('-t', '--telephone', dest='telephone')
def create_front_user(username, password, email):
    user = FrontUser.query.filter_by(username=username).first()
    if user:
        print('此名字用户名已经存在')
    user = FrontUser.query.filter_by(email=email).first()
    if user:
        print('此邮箱的用户已经存在')
    # user = FrontUser.query.filter_by(telephone=telephone).first()
    # if user:
    #     print('此手机号的用户已经存在')
    user = FrontUser(username=username, password=password, email=email)
    db.session.add(user)
    db.session.commit()
    print('前台用户添加成功')


if __name__ == '__main__':
    # manager.run()
    app.run()
